import '../../util/_base_export.dart';
import '../../util/_view_export.dart';

import 'package:flutter_form_builder/flutter_form_builder.dart';

import '../../util/app_util.dart';
import '../../blocs/cart.dart';

class OutofStockPage extends StatefulWidget {
// class OrderTimePage extends StatelessWidget {
  const OutofStockPage({Key? key}) : super(key: key);
  @override
  _OutofStockPageState createState() => _OutofStockPageState();
}

class _OutofStockPageState extends State<OutofStockPage> {
  @override
  initState() {
    super.initState();
  }

  int _choices = 0;

  GlobalKey<FormBuilderState> _fbKey = GlobalKey<FormBuilderState>();

  @override
  Widget build(BuildContext context) {
    CartBloc _cart = Provider.of<CartBloc>(context, listen: false);

    ValueChanged _onChangedDate = (val) {};

    return Scaffold(
      appBar: AppBar(
        brightness: Brightness.light,
        leading: GestureDetector(
          behavior: HitTestBehavior.opaque,
          onTap: () {
            Navigator.pop(context);
          },
          child: Container(
            child: Icon(
              Icons.chevron_left,
              color: AppColor.blackMain,
              size: 32,
            ),
          ),
        ),
        title: Text('如遇缺货', style: TextStyle(color: AppColor.blackMain)),
      ),
      body: Container(
        child: Column(
          children: [
            Container(
              height: _cart.ticket['outstock_options'].length * 86.h,
              child: ListView.builder(
                padding: EdgeInsets.all(0),
                physics: AlwaysScrollableScrollPhysics(),
                itemCount: _cart.ticket['outstock_options'].length ?? 0,
                itemBuilder: (BuildContext context, int index) {
                  var _item = _cart.ticket['outstock_options'][index];
                  return Column(children: [
                    Container(
                        height: 80.h,
                        child: Center(
                          child: RadioListTile(
                            title: Text(_item),
                            value: index,
                            groupValue: _choices,
                            onChanged: (value) {
                              setState(() {
                                _choices = index;
                              });
                            },
                            activeColor: Colors.green,
                          ),
                        )),
                    Container(height: 6.h, color: Color(0xffF4F4F4)),
                  ]);
                },
              ),
            ),
            SizedBox(height: 180.h),
            FlatButton(
              onPressed: () async {
                Map<String, dynamic> lm_param = {};
                lm_param = {
                  'selected_outstock_option': _choices,
                };
                var lv_result = await _cart.checkoutUpdate(lm_param);

                if (lv_result)
                  Navigator.pop(context);
                else
                  Navigator.pop(context);
              },
              child: Container(
                  width: 300.w,
                  height: 60.h,
                  decoration: BoxDecoration(
                      color: Color(0xffFFB91A),
                      borderRadius: BorderRadius.circular(30)),
                  child: Center(
                    child: Text(
                      '确认',
                      style: TextStyle(color: Colors.black, fontSize: 18.sp),
                    ),
                  )),
            ),
          ],
        ),
      ),
    );
  }
}
